package com.jy.modules.cims.component.jieyue;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.jy.modules.cims.common.CimsConstant;
import com.jy.modules.cims.common.CimsIrtUrlConstant;
import com.jy.modules.cims.common.open.api.enums.EntryResponseEnum;
import com.jy.modules.cims.common.sysUtils.CollectionsUtils;
import com.jy.modules.cims.common.sysUtils.SearchParamUtil;
import com.jy.modules.cims.component.api.IJieYueSend;
import com.jy.modules.cims.component.loan.TBJCLoanImpl;
import com.jy.modules.cims.data.dao.LoanDao;
import com.jy.modules.cims.data.dto.LoanDetailDto;
import com.jy.modules.cims.data.interact.api.JieYueResponseData;
import com.jy.modules.cims.data.interact.api.response.ResponseAuditResultSearchDTO;

@Service("com.jy.modules.cims.component.jieyue.CSynProcessAuditResultImpl")
public class CSynProcessAuditResultImpl implements ICSynProcess {

	private static final Logger logger = LoggerFactory.getLogger(CSynProcessAuditResultImpl.class);
	
	@Autowired
	@Qualifier("jieYueSendService")
	private IJieYueSend jieYueSend;
	
	@Autowired
	@Qualifier("com.jy.modules.cims.data.dao.LoanDao")
	private LoanDao loanDao;
	
	@Autowired
	@Qualifier("com.jy.modules.cims.component.loan.TBJCLoanImpl")
	private TBJCLoanImpl tbjCLoanImpl;

	/**
	 * @Description 根据机构编码，查询系统需要同步更新审批状态的贷款明细。 调用外围系统接口查询审批结果。更新系统贷款明细的审批状态。
	 * @param cooperorgCode
	 *            机构编码
	 * @throws Exception
	 * @author zhangmi
	 * @date 2016-10-10
	 */
	@Override
	public void doSomething(String cooperorgCode) throws Exception {
		logger.info("查询审批结果接口开始！");
		LoanDetailDto searchDto = new LoanDetailDto();
		searchDto.setCooperorgCode(cooperorgCode);//合作机构
		searchDto.setLoanStatus(CimsConstant.LOAN_WAY);//贷款状态-在途
		searchDto.setLoanEachStatus(null);//贷款交互状态-
		List<LoanDetailDto> list = loanDao.searchLoanByCooCodeAndSatus(SearchParamUtil.getSearchParam(searchDto));
		if(CollectionsUtils.isNotEmpty(list)){
			String contractNo = null;
			for (LoanDetailDto loanDetailDto : list) {
				contractNo = loanDetailDto.getContractNo();
				JieYueResponseData responseData = jieYueSend.searchAuditResult(cooperorgCode, contractNo);
				if(responseData!=null){
					if(EntryResponseEnum.SUCESS21001.getCode().equals(responseData.getRetCode())){
						ResponseAuditResultSearchDTO result = (ResponseAuditResultSearchDTO)responseData.getData();
						if(CimsConstant.JIEYUE_RESULT_API_AUDIT_SUCCESS.equals(result.getResult())){
							tbjCLoanImpl.auditLoan(contractNo, CimsConstant.LOAN_EACH_STATUS_AUDIT, CimsConstant.LOAN_CONFIRM_WIN,null);
							tbjCLoanImpl.approveConfrim(contractNo, CimsIrtUrlConstant.IRT_LOAN_APPROVE_SUCCESS);
						}else if(CimsConstant.JIEYUE_RESULT_API_AUDIT_REFUSE.equals(result.getResult())){
							tbjCLoanImpl.auditLoan(contractNo, CimsConstant.LOAN_EACH_STATUS_AUDIT, CimsConstant.LOAN_CONFIRM_LOSE,null);
							tbjCLoanImpl.approveConfrim(contractNo, CimsIrtUrlConstant.IRT_LOAN_APPROVE_FAIL);
						}else{
							logger.info("合作机构【{}】，查询审批结果异常编码【{}】，异常描述【{}】，合同号【{}】！",cooperorgCode,result.getResult(),result.getMessage(),contractNo);
						}
					}else{
						logger.info("合作机构【{}】，查询审批结果接口异常编码【{}】，异常描述【{}】，合同号【{}】！",cooperorgCode,responseData.getRetCode(),responseData.getMessage(),contractNo);
					}
				}else{
					logger.info("合作机构【{}】，查询审批状态接口，返回数据为空！",cooperorgCode);
				}
			}
		}else{
			logger.info("合作机构【{}】，没有需要查询审批结果的数据！",cooperorgCode);
		}
		logger.info("查询审批结果接口结束！");
	}

}
